package fr.generali.accueilclient.gwt.client.ui.components.table;

import java.util.List;

import com.google.common.collect.Lists;
import com.google.gwt.core.client.GWT;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class TableWidget<T> extends Composite {

    interface TableUiBinder extends UiBinder<Widget, TableWidget< ? >> {
    }

    @UiField
    Label infoLabel;

    @UiField(provided = true)
    CellTable<T> table;

    private List<T> data;

    private final List<ColumnDescription> descriptions;

    public TableWidget(List<ColumnDescription> descriptions) {
        this.descriptions = descriptions;
        table = createTable();
        initWidget(((TableUiBinder ) GWT.create(TableUiBinder.class)).createAndBindUi(this));
        data = Lists.newArrayList();
    }

    private CellTable<T> createTable() {
        table = new CellTable<T>(25, TableResources.INSTANCE);
        ColWidthSetter<T> widthSetter = GWT.create(ColWidthSetter.class);
        for (ColumnDescription description : descriptions) {
            Column<T, ? > column = description.getColumn();
            table.addColumn(column, SafeHtmlUtils.fromTrustedString(description.getName()));
            if (description.getStyleName() != null) {
                final int indexOf = descriptions.indexOf(column);
                table.addColumnStyleName(indexOf, description.getStyleName());
            }
            widthSetter.setColWidth(description, table, column);
        }
        return table;
    }

    public void setData(List<T> data) {
        if (data != null && !data.isEmpty()) {
            this.data = data;
            table.setRowCount(data.size(), true);
            table.setRowData(0, data);
            table.setVisible(true);
            infoLabel.setVisible(false);
            return;
        }
        table.setVisible(false);
        infoLabel.setVisible(true);
    }

    public List<T> getData() {
        return data;
    }

    public void addRowData(T rowData) {
        data.add(rowData);
        refresh();
    }

    public void removeRowData(T rowData) {
        if (data != null && !data.isEmpty()) {
            data.remove(rowData);

        }
        refresh();
    }

    public void setInfoLabel(String info) {
        infoLabel.setText(info);
    }

    public void setData(List<T> data, String info) {
        infoLabel.setText(info);
        setData(data);
    }

    public void setPageSize(int pageSize) {
        table.setPageSize(pageSize);
    }

    public CellTable<T> getCellTable() {
        return table;
    }

    public void refresh() {
        setData(data);
    }

    public void clear() {
        this.data = Lists.newArrayList();
        refresh();
    }

    public void setTableWidth(String width) {
        table.setWidth(width);
    }

}
